package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.album.TrackStatMqVo;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
@Slf4j
public class SearchReceiver {
    @Autowired
    private SearchService searchService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 监听专辑上架队列，完成专辑上架
     * @param albumId
     * @param message
     * @param channel
     */
    @RabbitListener(
            bindings = @QueueBinding(
                    exchange =@Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true", autoDelete = "true"),
                    value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER, durable = "true", autoDelete = "true"),
                    key = MqConst.ROUTING_ALBUM_UPPER
            ))
    @SneakyThrows
    public void upperAlbum(Long albumId, Message message, Channel channel) {
        if (albumId != null){
            log.info("[搜索服务]监听到专辑上架消息：{}", albumId);
            searchService.upperAlbum(albumId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     *
     * 监听专辑下架队列，完成专辑下架
     * @param albumId 专辑ID
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM, durable = "true", autoDelete = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER, durable = "true", autoDelete = "true"),
            key = MqConst.ROUTING_ALBUM_LOWER
    ))
    public void albumLower(Long albumId, Message message, Channel channel) {
        if (albumId != null) {
            log.info("[搜索服务]监听到专辑下架消息：{}", albumId);
            searchService.lowerAlbum(albumId);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_TRACK, durable = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_ES_STAT_UPDATE, durable = "true"),
            key = MqConst.ROUTING_TRACK_STAT_UPDATE
    ))
    @SneakyThrows
    public void updateAlbumStat(TrackStatMqVo trackStatMqVo, Message message, Channel channel) {
        if (trackStatMqVo != null){
            log.info("[搜索服务]监听到声音统计消息：{}", trackStatMqVo);
            String redisKey = RedisConstant.BUSINESS_PREFIX + "es" +  trackStatMqVo.getBusinessNo();
            Boolean flag = redisTemplate.opsForValue().setIfAbsent(redisKey, "1", 10, TimeUnit.MINUTES);
            if (flag){
                try {
                    searchService.updateAlbumStat(trackStatMqVo);
                } catch (Exception e) {
                    redisTemplate.delete(redisKey);
                    channel.basicNack(message.getMessageProperties().getDeliveryTag(),false, true);
                    return;
                }
            }
        }

    }

}
